package com.cnap.service.impl;

import com.cnap.CnapApplicationTests;
import com.cnap.db.entity.IndicatorPO;
import com.cnap.db.mapper.IndicatorMapper;
import com.cnap.model.indicator.QueryIndicatorParam;
import com.cnap.service.ExportService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;


class ExportServiceImplTest extends CnapApplicationTests {

    @Autowired
    private ExportService exportService;
    @MockBean
    private IndicatorExportServer indicatorExportServer;
    @MockBean
    private IndicatorMapper indicatorMapper;

    @Test
    void exportIndicator() throws IOException {
        HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
        HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
        Mockito.when(response.getOutputStream()).thenReturn(new ServletOutputStream() {
            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setWriteListener(WriteListener writeListener) {

            }

            @Override
            public void write(int b) throws IOException {

            }
        });

        Mockito.when(indicatorExportServer.selectListForExcelExport(Mockito.any(), Mockito.anyInt())).thenReturn(Collections.emptyList());

        Mockito.when(indicatorMapper.selectIndicatorByParam(Mockito.any(QueryIndicatorParam.class))).thenReturn(Collections.singletonList(new IndicatorPO()));

        Assertions.assertDoesNotThrow(()->{
            exportService.exportIndicator(request, response);
        });
    }
}